<?php
declare (strict_types=1);

namespace app\controller\index\hqwx;

// 处理环球网校
class Index extends \app\BaseController
{
    // 下载成功后，合并并重命名
    public function mergeAndReName()
    {
        $folder = 'E:\迅雷下载\2024中级工商高频考点基础班';
        $folder_info = \util\File::get_dirs($folder);
        if (count($folder_info['dir']) == count($folder_info['file'])) {
            halt('处理完毕');
        }
        $dir_list = $folder_info['dir'];

        foreach ($dir_list as $dir) {
            if ($dir == '.' || $dir == '..') {
                continue;
            }

            $m3u8_path = $folder . '/' . $dir . '/index.m3u8';

            $title = str_replace('.m3u8', '', $dir);
            $mp4_path = $folder . '/' . $title . '.mp4';
            if (file_exists($mp4_path)) {
                continue;
            }

            $command = 'ffmpeg -allowed_extensions ALL -i "' . $m3u8_path . '" -c copy "' . $mp4_path . '"';
            exec($command, $output, $return_var);
            if ($return_var === 0) {
                dump("成功将 $m3u8_path 合并为 $mp4_path");
            } else {
                dump("合并 $m3u8_path 失败，错误信息: " . implode("\n", $output));
            }
        }
    }

}
